package com.xiaoxu.springboot.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.springboot.modules.common.vo.Result;
import com.xiaoxu.springboot.modules.common.vo.Search;
import com.xiaoxu.springboot.modules.test.dao.CityDao;
import com.xiaoxu.springboot.modules.test.dao.CityDao2;
import com.xiaoxu.springboot.modules.test.entity.City;
import com.xiaoxu.springboot.modules.test.service.CityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityDao cityDao;
    @Autowired
    private CityDao2 cityDao2;

    @Override
    public Result<City> insertCity(City city) {
        City temp=cityDao.getCityByCityName(city.getCityName());
        if (temp!=null){
            return new Result<>(Result.ResultStatus.FAILD.code,"error");
        }else {
            city.setDateCreated(new Date());
            city.setDateModified(new Date());
            cityDao.insert(city);
            return new Result<>(Result.ResultStatus.SUCCESS.code,"success");

        }
    }

    @Override
    public Result<City> updateCity(City city) {
        City temp=cityDao.getCityByCityName(city.getCityName());
        if (temp!=null){
            return new Result<>(Result.ResultStatus.FAILD.code,"error");
        }else {
            cityDao.updateCity(city);
            return new Result<>(Result.ResultStatus.SUCCESS.code,"success");
        }

    }

    @Override
    public Result<Object> deleteCityByCityId(Integer cityId) {
        cityDao.deleteCityByCityId(cityId);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"success");
    }

    @Override
    public City selectCityByCityId(Integer cityId) {
        return cityDao.selectCityByCityId(cityId);
    }

    @Override
    public City getCityByNames(String cityName, String localCityName) {
        return cityDao.getCityByNames(cityName,localCityName);

    }

    @Override
    public List<City> getCitiesByCountryId(Integer countryId) {

        return Optional.ofNullable(cityDao.getCitiesByCountryId(countryId)).orElse(Collections.emptyList());
    }

    @Override
    public PageInfo<City> getCitiesBySeach(Search search) {
        search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return new PageInfo<>(Optional.ofNullable(cityDao.getCitiesBySeach(search))
                .orElse(Collections.emptyList())
        );
    }

    @Override
    public Page<City> getCitiesBySeach1(Search search) {
        search.init();
        Page<City> page=new Page<>(search.getCurrentPage(),search.getPageSize());
        QueryWrapper<City> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(
                StringUtils.isNotBlank(search.getKeyword()),
                "city_name",
                search.getKeyword()
        ).or().like(
                StringUtils.isNotBlank(search.getKeyword()),
                "local_city_name",
                search.getKeyword()
        );
        queryWrapper.orderBy(
                true,
                StringUtils.isBlank(search.getDirection())||
                        search.getDirection().toLowerCase().equals("asc"),
                search.getSort()
                );
        return cityDao2.selectPage(page,queryWrapper);
    }
}
